//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics;
using OpenLS.Spreadsheet.Formats.Biff.Records;
using OpenLS.Spreadsheet.Formats.Biff.Tokens;

namespace OpenLS.Spreadsheet.Formats.Biff
{
    
    internal class FormulaEvaluationContext
    {
        private readonly Stack<FormulaToken> _arguments;
        private readonly BiffReaderWorkbookContext _workbookContext;
        private readonly Cell _target;
        private readonly Sheet _worksheet;
        private readonly BiffReaderWorksheetContext _worksheetContext;
        private CFFormulaContext _conditionalFormatting;

        public FormulaEvaluationContext(BiffReaderWorksheetContext worksheetContext, Stack<FormulaToken> arguments, Cell target)
            : this(worksheetContext.WorkbookContext, arguments, target.Worksheet)
        {
            _target = target;
            _worksheetContext = worksheetContext;
        }

        public FormulaEvaluationContext(BiffReaderWorkbookContext context, Stack<FormulaToken> arguments,
                                        Sheet worksheet)
            : this(context, arguments, worksheet.Workbook)
        {
            _worksheet = worksheet;
        }

        public FormulaEvaluationContext(BiffReaderWorkbookContext context, Stack<FormulaToken> arguments,
                                        Workbook workbook)
        {
            _arguments = arguments;
            _workbookContext = context;
        }

        public FormulaEvaluationContext(BiffReaderWorksheetContext worksheetContext, Stack<FormulaToken> arguments,
                                        Sheet worksheet)
            : this(worksheetContext.WorkbookContext, arguments, worksheet.Workbook)
        {
            _worksheetContext = worksheetContext;
            _worksheet = _worksheetContext.Worksheet;
        }

        public FormulaEvaluationContext(BiffReaderWorksheetContext worksheetContext, Stack<FormulaToken> arguments, CFFormulaContext conditionalFormatting)
            : this(worksheetContext, arguments, conditionalFormatting.Cell)
        {
            this._conditionalFormatting = conditionalFormatting;
        }

        public BiffReaderWorksheetContext WorksheetContext
        {
            get { return _worksheetContext; }
        }

        public BiffReaderWorkbookContext WorkbookContext
        {
            get
            {
                if (_worksheetContext != null)
                    return _worksheetContext.WorkbookContext;
                return _workbookContext;
            }
        }

        public Worksheet Worksheet
        {
            get { return (Worksheet)_worksheet; }
        }

        public Workbook Workbook
        {
            get { return WorkbookContext.Workbook; }
        }

        public Stack<FormulaToken> Arguments
        {
            get { return _arguments; }
        }
        public FormulaToken PoppedArgument()
        {
            var result =  Arguments.Pop();
            //Debug.WriteLine("Popping " + result);
            return result;
        }

        public Cell Target
        {
            get { return _target; }
        }

        public bool InConditionalFormatting
        {
            get { return this._conditionalFormatting != null; }
        }
    }
}